package com.assistant.codegen.modules.model.app;

import com.assistant.codegen.modules.model.app.cmd.ModelInsertCmd;
import com.assistant.codegen.modules.model.app.cmd.ModelUpdateCmd;
import com.assistant.codegen.modules.model.domain.Model;
import com.assistant.codegen.modules.model.domain.gateway.ModelGateway;
import com.assistant.framework.PageUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ModelService {

    @Autowired
    private ModelGateway gateway;

    public Model get(Long id) {
        return gateway.getById(id);
    }

    public List<Model> list() {
        return gateway.findAll();
    }

    public Page<Model> page() {
        return gateway.findAll(PageUtils.currentPage());
    }

    public Model insert(ModelInsertCmd cmd) {
        Model model = new Model();
        BeanUtils.copyProperties(cmd, model);
        return gateway.save(model);
    }

    public void update(ModelUpdateCmd cmd) {
        Model model = gateway.getById(cmd.getId());
        BeanUtils.copyProperties(cmd, model);
        gateway.save(model);
    }

    public void delete(Long id) {
        gateway.deleteById(id);
    }
}
